home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / diff.zip / CONTEXT.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  14KB  |  488 lines

  1. /* Context-format output routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21. This port is also distributed under the terms of the GNU General
  22. Public License as published by the Free Software Foundation.
  23.  
  24. Please note that this file is not identical to the original GNU release,
  25. you should have received this code as patch to the official release.
  26.  
  27. $Header: e:/gnu/diff/RCS/context.c 1.15.0.2 91/03/12 17:06:06 tho Exp $  */
  28.  
  29. #include "diff.h"
  30. #include "regex.h"
  31.  
  32. #ifdef __STDC__
  33. static void print_context_number_range (struct file_data *file, int a, int b);
  34. static void pr_context_hunk (struct change *hunk);
  35. static void pr_unidiff_hunk (struct change *hunk);
  36. static struct change *find_hunk (struct change *start);
  37. static void mark_ignorable (struct change *script);
  38. static void find_function (struct file_data *file, int linenum,
  39.                char **linep, int *lenp);
  40. static void print_context_label (const char *mark, struct file_data *inf,
  41.                  const char *label);
  42. static void print_unidiff_number_range (struct file_data *file, int a, int b);
  43. #else /* not __STDC__ */
  44. static void pr_context_hunk ();
  45. static void pr_unidiff_hunk ();
  46. static struct change *find_hunk ();
  47. static void mark_ignorable ();
  48. static void find_function ();
  49. #endif /* not __STDC__ */
  50.  
  51. /* Last place find_function started searching from.  */
  52. static int find_function_last_search;
  53.  
  54. /* The value find_function returned when it started searching there.  */
  55. static int find_function_last_match;
  56.  
  57. /* Print a label for a context diff, with a file name and date or a label.  */
  58.  
  59. static void
  60. print_context_label (mark, inf, label)
  61.      const char *mark;
  62.      struct file_data *inf;
  63.      const char *label;
  64. {
  65.   if (label)
  66.     fprintf (outfile, "%s %s\n", mark, label);
  67.   else if (inf->stat.st_mtime)
  68.     fprintf (outfile, "%s %s\t%s", mark, inf->name, ctime(&inf->stat.st_mtime));
  69.   else
  70.     /* Don't pretend that standard input is ancient.  */
  71.     fprintf (outfile, "%s %s\n", mark, inf->name);
  72. }
  73.  
  74. /* Print a header for a context diff, with the file names and dates.  */
  75.  
  76. void
  77. print_context_header (inf, unidiff_flag)
  78.      struct file_data *inf;
  79.      int unidiff_flag;
  80. {
  81. #ifdef MSDOS
  82.   /* If we're dealing with empty files or the standard input,
  83.      the current time seems to be a reasonable approximation ....
  84.      MSC's ctime() returns NULL for invalid times, this breaks
  85.      the context diff format! */
  86.   if (!inf[0].stat.st_mtime)
  87.     time (&inf[0].stat.st_mtime);
  88.   if (!inf[1].stat.st_mtime)
  89.     time (&inf[1].stat.st_mtime);
  90. #endif /* MSDOS */
  91.  
  92.   if (unidiff_flag)
  93.     {
  94.       print_context_label ("---", &inf[0], file_label[0]);
  95.       print_context_label ("+++", &inf[1], file_label[1]);
  96.     }
  97.   else
  98.     {
  99.       print_context_label ("***", &inf[0], file_label[0]);
  100.       print_context_label ("---", &inf[1], file_label[1]);
  101.     }
  102. }
  103.  
  104. /* Print an edit script in context format.  */
  105.  
  106. void
  107. print_context_script (script, unidiff_flag)
  108.      struct change *script;
  109.      int unidiff_flag;
  110. {
  111.   if (ignore_blank_lines_flag || ignore_regexp)
  112.     mark_ignorable (script);
  113.   else
  114.     {
  115.       struct change *e;
  116.       for (e = script; e; e = e->link)
  117.     e->ignore = 0;
  118.     }
  119.  
  120.   find_function_last_search = 0;
  121.   find_function_last_match = -1;
  122.  
  123.   if (unidiff_flag)
  124.     print_script (script, find_hunk, pr_unidiff_hunk);
  125.   else
  126.     print_script (script, find_hunk, pr_context_hunk);
  127. }
  128.  
  129. /* Print a pair of line numbers with a comma, translated for file FILE.
  130.    If the second number is not greater, use the first in place of it.
  131.  
  132.    Args A and B are internal line numbers.
  133.    We print the translated (real) line numbers.  */
  134.  
  135. static void
  136. print_context_number_range (file, a, b)
  137.      struct file_data *file;
  138.      int a, b;
  139. {
  140.   int trans_a, trans_b;
  141.   translate_range (file, a, b, &trans_a, &trans_b);
  142.  
  143.   /* Note: we can have B < A in the case of a range of no lines.
  144.      In this case, we should print the line number before the range,
  145.      which is B.  */
  146.   if (trans_b > trans_a)
  147.     fprintf (outfile, "%d,%d", trans_a, trans_b);
  148.   else
  149.     fprintf (outfile, "%d", trans_b);
  150. }
  151.  
  152. /* Print a portion of an edit script in context format.
  153.    HUNK is the beginning of the portion to be printed.
  154.    The end is marked by a `link' that has been nulled out.
  155.  
  156.    Prints out lines from both files, and precedes each
  157.    line with the appropriate flag-character.  */
  158.  
  159. static void
  160. pr_context_hunk (hunk)
  161.      struct change *hunk;
  162. {
  163.   int first0, last0, first1, last1, show_from, show_to, i;
  164.   struct change *next;
  165.   char *prefix;
  166.   char *function;
  167.   int function_length;
  168.  
  169.   /* Determine range of line numbers involved in each file.  */
  170.  
  171.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  172.  
  173.   if (!show_from && !show_to)
  174.     return;
  175.  
  176.   /* Include a context's width before and after.  */
  177.  
  178.   first0 = max (first0 - context, 0);
  179.   first1 = max (first1 - context, 0);
  180.   last0 = min (last0 + context, files[0].buffered_lines - 1);
  181.   last1 = min (last1 + context, files[1].buffered_lines - 1);
  182.  
  183.   /* If desired, find the preceding function definition line in file 0.  */
  184.   function = 0;
  185.   if (function_regexp)
  186.     find_function (&files[0], first0, &function, &function_length);
  187.  
  188.   /* If we looked for and found a function this is part of,
  189.      include its name in the header of the diff section.  */
  190.   fprintf (outfile, "***************");
  191.  
  192.   if (function)
  193.     {
  194.       fprintf (outfile, " ");
  195.       fwrite (function, 1, min (function_length - 1, 40), outfile);
  196.     }
  197.  
  198.   fprintf (outfile, "\n*** ");
  199.   print_context_number_range (&files[0], first0, last0);
  200.   fprintf (outfile, " ****\n");
  201.  
  202.   if (show_from)
  203.     {
  204.       next = hunk;
  205.  
  206.       for (i = first0; i <= last0; i++)
  207.     {
  208.       /* Skip past changes that apply (in file 0)
  209.          only to lines before line I.  */
  210.  
  211.       while (next && next->line0 + next->deleted <= i)
  212.         next = next->link;
  213.  
  214.       /* Compute the marking for line I.  */
  215.  
  216.       prefix = " ";
  217.       if (next && next->line0 <= i)
  218.         /* The change NEXT covers this line.
  219.            If lines were inserted here in file 1, this is "changed".
  220.            Otherwise it is "deleted".  */
  221.         prefix = (next->inserted > 0 ? "!" : "-");
  222.  
  223.       print_1_line (prefix, &files[0].linbuf[i]);
  224.     }
  225.     }
  226.  
  227.   fprintf (outfile, "--- ");
  228.   print_context_number_range (&files[1], first1, last1);
  229.   fprintf (outfile, " ----\n");
  230.  
  231.   if (show_to)
  232.     {
  233.       next = hunk;
  234.  
  235.       for (i = first1; i <= last1; i++)
  236.     {
  237.       /* Skip past changes that apply (in file 1)
  238.          only to lines before line I.  */
  239.  
  240.       while (next && next->line1 + next->inserted <= i)
  241.         next = next->link;
  242.  
  243.       /* Compute the marking for line I.  */
  244.  
  245.       prefix = " ";
  246.       if (next && next->line1 <= i)
  247.         /* The change NEXT covers this line.
  248.            If lines were deleted here in file 0, this is "changed".
  249.            Otherwise it is "inserted".  */
  250.         prefix = (next->deleted > 0 ? "!" : "+");
  251.  
  252.       print_1_line (prefix, &files[1].linbuf[i]);
  253.     }
  254.     }
  255. }
  256.  
  257. /* Print a pair of line numbers with a comma, translated for file FILE.
  258.    If the second number is smaller, use the first in place of it.
  259.    If the numbers are equal, print just one number.
  260.  
  261.    Args A and B are internal line numbers.
  262.    We print the translated (real) line numbers.  */
  263.  
  264. static void
  265. print_unidiff_number_range (file, a, b)
  266.      struct file_data *file;
  267.      int a, b;
  268. {
  269.   int trans_a, trans_b;
  270.   translate_range (file, a, b, &trans_a, &trans_b);
  271.  
  272.   /* Note: we can have B < A in the case of a range of no lines.
  273.      In this case, we should print the line number before the range,
  274.      which is B.  */
  275.   if (trans_b <= trans_a)
  276.     fprintf (outfile, trans_b == trans_a ? "%d" : "%d,0", trans_b);
  277.   else
  278.     fprintf (outfile, "%d,%d", trans_a, trans_b - trans_a + 1);
  279. }
  280.  
  281. /* Print a portion of an edit script in unidiff format.
  282.    HUNK is the beginning of the portion to be printed.
  283.    The end is marked by a `link' that has been nulled out.
  284.  
  285.    Prints out lines from both files, and precedes each
  286.    line with the appropriate flag-character.  */
  287.  
  288. static void
  289. pr_unidiff_hunk (hunk)
  290.      struct change *hunk;
  291. {
  292.   int first0, last0, first1, last1, show_from, show_to, i, j, k;
  293.   struct change *next;
  294.   int lastline;
  295.   char *function;
  296.   int function_length;
  297.  
  298.   /* Determine range of line numbers involved in each file.  */
  299.  
  300.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  301.  
  302.   if (!show_from && !show_to)
  303.     return;
  304.  
  305.   /* Include a context's width before and after.  */
  306.  
  307.   first0 = max (first0 - context, 0);
  308.   first1 = max (first1 - context, 0);
  309.   last0 = min (last0 + context, files[0].buffered_lines - 1);
  310.   last1 = min (last1 + context, files[1].buffered_lines - 1);
  311.  
  312.   /* If desired, find the preceding function definition line in file 0.  */
  313.   function = 0;
  314.   if (function_regexp)
  315.     find_function (&files[0], first0, &function, &function_length);
  316.  
  317.   /* If we looked for and found a function this is part of,
  318.      include its name in the header of the diff section.  */
  319.  
  320.   fprintf (outfile, "@@ -");
  321.   print_unidiff_number_range (&files[0], first0, last0);
  322.   fprintf (outfile, " +");
  323.   print_unidiff_number_range (&files[1], first1, last1);
  324.   fprintf (outfile, " @@");
  325.  
  326.   if (function)
  327.     {
  328.       putc (' ', outfile);
  329.       fwrite (function, 1, min (function_length - 1, 40), outfile);
  330.     }
  331.   putc ('\n', outfile);
  332.  
  333.   next = hunk;
  334.   i = first0;
  335.   j = first1;
  336.  
  337.   while (i <= last0 || j <= last1)
  338.     {
  339.  
  340.       /* If the line isn't a difference, output the context from file 0. */
  341.  
  342.       if (!next || i < next->line0)
  343.     {
  344.       putc (' ', outfile);
  345.       print_1_line ((char *)0, &files[0].linbuf[i++]);
  346.       j++;
  347.     }
  348.       else
  349.     {
  350.       /* For each difference, first output the deleted part. */
  351.  
  352.       k = next->deleted;
  353.       while (k--)
  354.         {
  355.           putc ('-', outfile);
  356.           print_1_line ((char *)0, &files[0].linbuf[i++]);
  357.         }
  358.  
  359.       /* Then output the inserted part. */
  360.  
  361.       k = next->inserted;
  362.       while (k--)
  363.         {
  364.           putc ('+', outfile);
  365.           print_1_line ((char *)0, &files[1].linbuf[j++]);
  366.         }
  367.  
  368.       /* We're done with this hunk, so on to the next! */
  369.  
  370.       next = next->link;
  371.     }
  372.     }
  373. }
  374.  
  375. /* Scan a (forward-ordered) edit script for the first place that at least
  376.    2*CONTEXT unchanged lines appear, and return a pointer
  377.    to the `struct change' for the last change before those lines.  */
  378.  
  379. static struct change *
  380. find_hunk (start)
  381.      struct change *start;
  382. {
  383.   struct change *prev;
  384.   int top0, top1;
  385.   int thresh;
  386.  
  387.   do
  388.     {
  389.       /* Computer number of first line in each file beyond this changed.  */
  390.       top0 = start->line0 + start->deleted;
  391.       top1 = start->line1 + start->inserted;
  392.       prev = start;
  393.       start = start->link;
  394.       /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  395.      but only CONTEXT if one is ignorable.  */
  396.       thresh = ((prev->ignore || (start && start->ignore))
  397.         ? context
  398.         : 2 * context);
  399.       /* It is not supposed to matter which file we check in the end-test.
  400.      If it would matter, crash.  */
  401.       if (start && start->line0 - top0 != start->line1 - top1)
  402.     abort ();
  403.     } while (start
  404.          /* Keep going if less than THRESH lines
  405.         elapse before the affected line.  */
  406.          && start->line0 < top0 + thresh);
  407.  
  408.   return prev;
  409. }
  410.  
  411. /* Set the `ignore' flag properly in each change in SCRIPT.
  412.    It should be 1 if all the lines inserted or deleted in that change
  413.    are ignorable lines.  */
  414.  
  415. static void
  416. mark_ignorable (script)
  417.      struct change *script;
  418. {
  419.   while (script)
  420.     {
  421.       struct change *next = script->link;
  422.       int first0, last0, first1, last1, deletes, inserts;
  423.  
  424.       /* Turn this change into a hunk: detach it from the others.  */
  425.       script->link = 0;
  426.  
  427.       /* Determine whether this change is ignorable.  */
  428.       analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
  429.       /* Reconnect the chain as before.  */
  430.       script->link = next;
  431.  
  432.       /* If the change is ignorable, mark it.  */
  433. #ifdef MSDOS
  434.       script->ignore = (char) (!deletes && !inserts);
  435. #else /* not MSDOS */
  436.       script->ignore = (!deletes && !inserts);
  437. #endif /* not MSDOS */
  438.  
  439.       /* Advance to the following change.  */
  440.       script = next;
  441.     }
  442. }
  443.  
  444. /* Find the last function-header line in FILE prior to line number LINENUM.
  445.    This is a line containing a match for the regexp in `function_regexp'.
  446.    Store the address of the line text into LINEP and the length of the
  447.    line into LENP.
  448.    Do not store anything if no function-header is found.  */
  449.  
  450. static void
  451. find_function (file, linenum, linep, lenp)
  452.      struct file_data *file;
  453.      int linenum;
  454.      char **linep;
  455.      int *lenp;
  456. {
  457.   int i = linenum;
  458.   int last = find_function_last_search;
  459.   find_function_last_search = i;
  460.  
  461.   while (--i >= last)
  462.     {
  463.       /* See if this line is what we want.  */
  464.  
  465.       if (0 <= re_search (&function_regexp_compiled,
  466.               file->linbuf[i].text,
  467.               file->linbuf[i].length,
  468.               0, file->linbuf[i].length,
  469.               0))
  470.     {
  471.       *linep = file->linbuf[i].text;
  472.       *lenp = file->linbuf[i].length;
  473.       find_function_last_match = i;
  474.       return;
  475.     }
  476.     }
  477.   /* If we search back to where we started searching the previous time,
  478.      find the line we found last time.  */
  479.   if (find_function_last_match >= 0)
  480.     {
  481.       i = find_function_last_match;
  482.       *linep = file->linbuf[i].text;
  483.       *lenp = file->linbuf[i].length;
  484.       return;
  485.     }
  486.   return;
  487. }
  488.